package com.jinzhi.common.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.enmu.LogOperationType;
import com.jinzhi.common.enmu.LogSysModule;
import com.jinzhi.common.utils.StringUtil;
import com.jinzhi.common.vo.LogDownLoadVo;
import com.jinzhi.jzweb.utils.EasyPoiUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.jinzhi.common.base.CoreServiceImpl;
import com.jinzhi.common.dao.LogDao;
import com.jinzhi.common.domain.LogDO;
import com.jinzhi.common.service.LogService;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * <pre>
 * </pre>
 * <small> 2018年3月22日 | Aron</small>
 */
@Service
public class LogServiceImpl extends CoreServiceImpl<LogDao, LogDO> implements LogService {


    /**
     * 获取日志编号
     *
     * @return
     */
    @Override
    public String getLogNo() {
        return baseMapper.selectLogNo();
    }

    /**
     * 分页查询日志列表
     *
     * @param logDTO
     * @return
     */
    @Override
    public Page<LogDO> getLogListPage(LogDO logDTO) {
        Page<LogDO> doPage = new Page<>(logDTO.getCurrent(), logDTO.getSize());
        EntityWrapper<LogDO> wrapper = new EntityWrapper<>();
        if (StringUtil.notEmpty(logDTO.getStartTime())) {
            wrapper.ge("gmtCreate",logDTO.getStartTime()); // createTime >= startTime
        }
        if (StringUtil.notEmpty(logDTO.getEndTime())) {
            wrapper.le("gmtCreate", logDTO.getEndTime()); // createTime <= endTime
        }
        if (logDTO.getIds() != null && logDTO.getIds().size()>0) {
            wrapper.in("id",logDTO.getIds());
        }
        wrapper.eq(logDTO.getSysModule()!=null, "sys_module", logDTO.getSysModule());
        wrapper.eq(logDTO.getOperationType()!=null, "operation_type", logDTO.getOperationType());
        wrapper.eq(logDTO.getStatus()!=null, "status", logDTO.getStatus());
        wrapper.like(StringUtil.notEmpty(logDTO.getUsername()),"username", logDTO.getUsername());
        Page<LogDO> page = selectPage(doPage, wrapper);
        return page;
    }

    /**
     * 日志导出
     *
     * @param response
     * @param logDTO
     */
    @Override
    public void downloadImportTemplate(HttpServletResponse response, LogDO logDTO) {
        List<LogDownLoadVo> list1 = new LinkedList<>();
        List<LogDO> list = this.getLogListPage(logDTO).getRecords();
        for(LogDO li:list){
            LogDownLoadVo logDownLoadVo=new LogDownLoadVo();
            BeanUtils.copyProperties(li,logDownLoadVo);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            // 使用format()方法将Date转换为字符串
            String formattedDate = sdf.format(li.getGmtCreate());
            logDownLoadVo.setGmtCreate(formattedDate);
            // 日志状态
            if (li.getStatus()!=null){
                if (li.getStatus() == 0){
                    logDownLoadVo.setStatus("成功");
                } else if (li.getStatus() == 1) {
                    logDownLoadVo.setStatus("失败");
                }
            } else {
                logDownLoadVo.setStatus(null);
            }
            // 系统模块
            if (li.getSysModule()!=null){
                switch (li.getSysModule()) {
                    case 1: logDownLoadVo.setSysModule(LogSysModule.USER_MANAGEMENT.getName()); break;
                    case 2: logDownLoadVo.setSysModule(LogSysModule.CONFINEMENT_MANAGEMENT.getName()); break;
                    case 3: logDownLoadVo.setSysModule(LogSysModule.DEMAND_MANAGEMENT.getName()); break;
                    case 4: logDownLoadVo.setSysModule("未知"); break;
                    case 5: logDownLoadVo.setSysModule(LogSysModule.ORDER_MANAGEMENT.getName()); break;
                    case 6: logDownLoadVo.setSysModule(LogSysModule.COMMODITY_MANAGEMENT.getName()); break;
                    case 7: logDownLoadVo.setSysModule(LogSysModule.CONTRACT_MANAGEMENT.getName()); break;
                    case 8: logDownLoadVo.setSysModule(LogSysModule.EVALUATION_MANAGEMENT.getName()); break;
                    case 9: logDownLoadVo.setSysModule(LogSysModule.LABEL_MANAGEMENT.getName()); break;
                    case 10: logDownLoadVo.setSysModule(LogSysModule.SERVICE_MANAGEMENT.getName()); break;
                    case 11: logDownLoadVo.setSysModule(LogSysModule.BANNER_MANAGEMENT.getName()); break;
                    case 12: logDownLoadVo.setSysModule(LogSysModule.BASIC_MANAGEMENT.getName()); break;
                    default: logDownLoadVo.setSysModule("未知"); break;
                }
            }else {
                logDownLoadVo.setSysModule(null);
            }
            // 操作类型
            if (li.getOperationType()!=null){
                switch (li.getOperationType()) {
                    case 1: logDownLoadVo.setOperationType(LogOperationType.ADD.getName()); break;
                    case 2: logDownLoadVo.setOperationType(LogOperationType.DELETE.getName()); break;
                    case 3: logDownLoadVo.setOperationType(LogOperationType.EDIT.getName()); break;
                    case 4: logDownLoadVo.setOperationType(LogOperationType.EXPORT.getName()); break;
                    case 5: logDownLoadVo.setOperationType(LogOperationType.IMPORT.getName()); break;
                }
            }else {
                logDownLoadVo.setOperationType(null);
            }
            list1.add(logDownLoadVo);
        }
        //导出操作
        EasyPoiUtil.exportExcel(list1, "日志导出", "日志导出", LogDownLoadVo.class, "日志导出", response);
    }
}
